<!--
 * @页面名称:管理全宗
 * @描述: 
 * @作者: 
 * @Date:
-->
<template>
  <div class="manageFonds">
    <!-- 按钮组 -->
    <!-- <div class="button-group">
      <el-button @click="save" type="primary">保存</el-button>
    </div> -->
    <div>
      <ztree :setting="treeSetting.setting" :nodes="treeSetting.treeData" @onCreated="onCreated"></ztree>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, nextTick, onMounted } from "vue";
import useCurrentInstance from "@/utils/useCurrentInstance";
import Ztree from "@/components/ztree/Ztree.vue";

const { proxy } = useCurrentInstance();
// 父级表格数据
const tabClickFromParent = ref();
// tree配置
const treeSetting = reactive({
  setting: {
    // ztree配置
    data: {
      simpleData: {
        enable: true,
        idKey: "Fonds_ID",
        pIdKey: "Fonds_Father"
      },
      key: {
        checked: "IsCheck",
        name: "Fonds_Name"
      }
    },
    check: {
      enable: true
    },
    view: {
      showIcon: false
    }
  },
  treeData: [],
  ztreeObj: {} as any
});
// 获取全宗数据
const getFondsData = (record: any) => {
  tabClickFromParent.value = record;
  proxy.$vueAxios.get("MasterPermission/GetRolePermissionFonds", { gId: record.G_ID }, { noLoading: true }).then((res: any) => {
    treeSetting.treeData = res.List;
  });
};
// ztree初始化事件
const onCreated = (ztreeObj: any) => {
  treeSetting.ztreeObj = ztreeObj;
};
// 新增数据
const addData = async (arr: any) => {
  const param = [] as any;
  arr.forEach((item: any) => {
    param.push({
      pmF_ID: proxy.$global.createID(),
      g_ID: tabClickFromParent.value.G_ID,
      fonds_ID: item.Fonds_ID
    });
  });
  return await proxy.$vueAxios.post("Master_Base_Permission_ManagerFonds/AddList", param, { noLoading: true });
};
// 删除数据
const delData = async () => {
  await proxy.$vueAxios.post(
    "Master_Base_Permission_ManagerFonds/DeleteByColumnName",
    { columnName: "G_ID", ids: [tabClickFromParent.value.G_ID] },
    { noLoading: true }
  );
};
// 保存
const save = async () => {
  proxy.$showLoading({ tip: "正在保存" });
  const treeArray = treeSetting.ztreeObj.transformToArray(treeSetting.ztreeObj.getCheckedNodes());
  await delData();
  const res: any = await addData(treeArray);
  if (res.Mark) {
    proxy.$message.success("保存成功");
  } else {
    proxy.$message.error("保存失败");
  }
  proxy.$hideLoading();
};
// 将函数暴露给父组件
defineExpose({ getFondsData, save });
</script>

<style scoped lang="scss">
.manageFonds {
  height: calc(100vh - 140px);
  padding-right: 6px;
  margin-left: 10px;
  overflow-y: scroll;
}
</style>
